package android.example;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;

public class CalendarDiary extends Activity implements View.OnClickListener{
	static final int DATE_DIALOG_ID=0;

	Button b1, b2;
	TextView tv;
	ArrayAdapter<String> mAdapter;
	ArrayList<String> diaryList;
	String[] diarys = {"aaaaa", "it"};
	//EditText nameText;
	
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.calendar);
		
		b1=(Button)findViewById(R.id.pickDate);
		b1.setOnClickListener(this);
		
		mAdapter=new ArrayAdapter<String>(getApplicationContext(), R.layout.list_row,
				diaryList);
		setListAdapter(mAdapter);
		
		diaryList = new ArrayList<String>();
		diaryList.addAll(Arrays.asList(diarys));
		//nameText=(EditText)findViewById(R.id.editText1);
		b2=(Button)findViewById(R.id.adddiary);
		b2.setOnClickListener(this);
	}
	
	private void setListAdapter(ArrayAdapter<String> mAdapter2) {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected Dialog onCreateDialog(int id){
		final Calendar c = Calendar.getInstance();
		int cyear = c.get(Calendar.YEAR);
		int cmonth = c.get(Calendar.MONTH);
		int cday = c.get(Calendar.DAY_OF_MONTH);
		
		
		switch(id){
		case DATE_DIALOG_ID:
			return new DatePickerDialog(this, mDateSetListener, cyear, cmonth, cday);
		}
		return null;
	}
	

	private DatePickerDialog.OnDateSetListener mDateSetListener = new OnDateSetListener(){
		public void onDateSet(DatePicker view, int year, int monthOfYear, int DayOfMonth){
			String date_selected = String.valueOf(year)+"."+String.valueOf(monthOfYear+1)
					+"."+String.valueOf(DayOfMonth);
			tv = (TextView)findViewById(R.id.dateDisplay);
			tv.setText(date_selected);
		}
	};
	/*
	 *
	 private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.onDateSetListener(){
		public void onDateSet(DatePicker view, int year, int monthOfYear, int DayOfMonth){
			String date_selected = String.valueOf(monthOfYear+1)+"."+String.valueOf(dayOfMonth)
					+"."+String.valueOf(year);
			tv = (TextView)findViewById(R.id.dateDisplay);
			tv.setText(date_selected);
		}
	};
	*/
	public void onClick(View view) {
		// TODO Auto-generated method stub
		
		if(view==b1)
			showDialog(DATE_DIALOG_ID);
		if(view==b2){
			EditText edit = (EditText)findViewById(R.id.editText1);
			//mAdapter.add(edit.getText().toString());
			diaryList.add(0, edit.getText().toString());
			mAdapter.notifyDataSetChanged();
		}
		
	}
}
